package com.apimarket.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.apimarket.entity.Apply;
import com.apimarket.entity.Result;
import com.apimarket.service.ApplyService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;

/**
 * xuyang
 */
@RestController
@RequestMapping("apply")
@CrossOrigin
public class ApplyController {

    @Autowired
    private ApplyService service;

    @RequestMapping("/dataset/add")
    public Result<String> addDatasetApply(Apply apply,MultipartFile picFile){
        service.addApply(apply,picFile);
        return new Result<>("数据块申请成功",200,"success");
    }

    /**
     * 只是用来测试给用户绑定rateLimiting插件
     * 实现对某个servcie的访问限流
     * @param userId
     * @param times
     * @param serviceId
     */
    @RequestMapping("ceshiBindRateLimiting")
    public void ceshiBindRateLimiting(String userId,Integer times,String serviceId) throws IOException, URISyntaxException {
        service.consumerBindRateLimit(userId,times,serviceId);
    }

    /**
     * 申请通过
     * 申请通过的话需要对这个用户做流控和接口集的权限放行
     * @return
     */
    @RequestMapping("/applyPass")
    public Result<String> applyPass(Apply apply) throws URISyntaxException {
        try {
            service.applyPass(apply);
            return new Result<String>("申请通过",200,"success");
        } catch (IOException e) {
            return new Result<String>("申请审核失败",400,"fail");
        }
    }

    /**
     * 驳回申请
     * @param apply
     * @return
     */
    @RequestMapping("/reject")
    public Result<String> applyReject(Apply apply){
        service.applyReject(apply);
        return new Result<>("驳回申请成功",200,"success");
    }

    /**
     * 分页条件查询申请
     * @param entity
     * @return
     */
    @GetMapping("/getPageObject")
    public Result<PageInfo<Apply>> getPageObject(Apply entity,Integer pageSize,Integer page){
        PageInfo<Apply> result=service.getPageObject(entity,pageSize,page);
        return new Result<>(result,200,"success");
    }
    @GetMapping("list")
    public String queryLists(Apply entity,Integer pageSize,Integer page){
        List<Apply> list = service.queryList(entity);
        String result = JSON.toJSONString(list, SerializerFeature.DisableCircularReferenceDetect);
        return result;
    }

    @RequestMapping("/add")
    public Result<String> addApply(Apply apply, MultipartFile picFile){
        service.addApply(apply,picFile);
        return new Result<>("success",200,"success");
    }

    /*@PostMapping("add")
    public ResponseEntity<List<Apply>> insert(@RequestBody Apply entity){
        service.insert(entity);
        return ResponseEntity.ok(service.queryList(new Apply()));
    }*/
    @PostMapping("update")
    public Result<String> update(Apply apply,List<MultipartFile> picFile){
        service.updateApply(apply,picFile);
        //service.update(entity);
        return new Result<>("更新成功",200,"success");
    }
    @GetMapping("delete")
    public Result<String> delete(Integer id){

        //todo 设置删除
        service.delete(id);
        return new Result<>("删除成功",200,"success");

    }
}
